package com.zhaojk.audio.service;

import com.zhaojk.audio.domain.SpeechRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

/**
 * 火山大模型语音服务实现
 *
 * @author zhaojk
 */
@Service
public class VolcAudioServiceImpl implements AudioService{
    /**
     * coze的语音接口URL
     */
    private static final String SPEECH_URL = "https://api.coze.cn/v1/audio/speech";
    /**
     * coze的授权码，有效期30天
     */
    private static final String BEARER_TOKEN = "pat_owHD7HmIzCDCRsMAweoYce1o6H5lIsNtyoGcAcOGtT6sOvY5cB2ZHsnBrjFSgSnt";

    @Autowired
    private RestTemplate restTemplate;

    @Override
    public byte[] generateAudioFromText(String text) {
        // 创建请求体
        SpeechRequest request = new SpeechRequest();
        request.setVoiceId("7426725529589579803");
        request.setSpeed(1.0);
        request.setInput(text);

        // 设置请求头
        HttpHeaders headers = new HttpHeaders();
        headers.setBearerAuth(BEARER_TOKEN);
        headers.setContentType(MediaType.APPLICATION_JSON);

        // 创建请求实体
        HttpEntity<SpeechRequest> entity = new HttpEntity<>(request, headers);

        // 发送请求并获取响应
        ResponseEntity<byte[]> response = restTemplate.exchange(
                SPEECH_URL,
                HttpMethod.POST,
                entity,
                byte[].class
        );

        return response.getBody();
    }

    @Override
    public String generateAudioFileFromText(String text) throws Exception {
        return null;
    }

    @Override
    public String generateAudioBase64FromText(String text, int repeat, boolean isRandom) throws Exception {
        return null;
    }

}
